<template>
  <div>
    <HomeHeader></HomeHeader>
    <HomeSwiper :swiperList="swiperList"></HomeSwiper>
    <HomeIcons :iconList="iconList"></HomeIcons>
    <HomeRecommand :recommendList="recommendList"></HomeRecommand>
    <HomeWeekend :weekendList="weekendList"></HomeWeekend>
  </div>
</template>

<script>
  import HomeHeader from './components/Header'
  import HomeSwiper from './components/Swiper'
  import HomeIcons from './components/Icons'
  import HomeRecommand from './components/Recommand'
  import HomeWeekend from './components/Weekend'
  import { mapState } from 'vuex'
  import axios from 'axios'

  export default {
    name: 'home',
    components: {
      HomeHeader,
      HomeSwiper,
      HomeIcons,
      HomeRecommand,
      HomeWeekend
    },
    data() {
      return {
        lastCity: '',
        iconList: [],
        recommendList: [],
        weekendList: [],
        swiperList: []
      }
    },
    computed: {
      ...mapState(['city'])
    },
    methods: {
      getHomeInfo() {
        axios.get('/static/mock/index.json?city=' + this.city)
          .then(this.getHomeInfoSuccess)
      },
      getHomeInfoSuccess(res) {
        const data = res.data.data;
        this.iconList = data.iconList;
        this.recommendList = data.recommendList;
        this.weekendList = data.weekendList;
        this.swiperList = data.swiperList;
      }
    },
    mounted() {
      this.lastCity = this.city
      this.getHomeInfo()
    },
    activated() {
      if (this.lastCity !== this.city) {
        this.lastCity = this.city
        this.getHomeInfo()
      }
    }
  }
</script>

<style>

</style>
